package com.ludaxu.zhbj;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.onekeyshare.OnekeyShareTheme;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.TextSize;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import android.widget.ProgressBar;

public class NewsDetailActivity extends Activity implements OnClickListener {

	private WebView mWebView;
	private ImageButton btnBack;
	private ImageButton btnSize;
	private ImageButton btnShare;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_news_detail);
		mWebView = (WebView) findViewById(R.id.wv_web);
		btnBack = (ImageButton) findViewById(R.id.btn_back);
		btnBack.setOnClickListener(this);
		btnSize = (ImageButton) findViewById(R.id.btn_size);
		btnSize.setOnClickListener(this);
		btnShare = (ImageButton) findViewById(R.id.btn_share);
		btnShare.setOnClickListener(this);
		final ProgressBar pbProgress = (ProgressBar) findViewById(R.id.pb_progress);

		String url = getIntent().getStringExtra("url");
		WebSettings settings = mWebView.getSettings();
		settings.setJavaScriptEnabled(true);// 表示支持JS.默认不支持
		settings.setBuiltInZoomControls(true);// 支持缩放
		settings.setUseWideViewPort(true);// 支持双击缩放
		mWebView.setWebViewClient(new WebViewClient() {
			// 网页开始加载的回调
			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				super.onPageStarted(view, url, favicon);
				System.out.println("网页开始加载!");
				pbProgress.setVisibility(View.VISIBLE);
			}

			// 网页加载结束
			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
				System.out.println("网页结束加载!");
				pbProgress.setVisibility(View.GONE);
			}

			// 所有跳转的链接都会在此方法回调
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				System.out.println("跳转url" + url);
				// 在activity中跳转网页,不调用浏览器
				view.loadUrl(url);
				return true;

			}

		});
		mWebView.setWebChromeClient(new WebChromeClient() {
			// 进度发生变化
			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				System.out.println("加载进度:" + newProgress);
				super.onProgressChanged(view, newProgress);
			}

			@Override
			public void onReceivedTitle(WebView view, String title) {
				System.out.println("网页标题:" + title);
				super.onReceivedTitle(view, title);
			}

		});
		mWebView.loadUrl(url);// 加载网页

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			finish();

			break;
		case R.id.btn_size:
			showChooseDialog();
			break;
		case R.id.btn_share:
			showShare();
			break;

		default:
			break;
		}
	}

	private int mCurrentChooseItem;// 记录将要选择的item
	private int mCurrentItem = 2;// 记录确定选择的item

	/**
	 * 显示选择对话框
	 */
	private void showChooseDialog() {
		final String[] items = new String[] { "超大号字体", "大号字体", "正常字体", "小号字体",
				"超小号字体" };
		AlertDialog.Builder builder = new AlertDialog.Builder(
				NewsDetailActivity.this);
		builder.setTitle("字体设置");
		builder.setSingleChoiceItems(items, mCurrentItem,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						System.out.println("选择了字体:" + items[which]);
						mCurrentChooseItem = which;
					}
				});
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			// 这里的which永远是0
			@Override
			public void onClick(DialogInterface dialog, int which) {
				WebSettings settings = mWebView.getSettings();
				switch (mCurrentChooseItem) {
				case 0:
					settings.setTextSize(TextSize.LARGEST);
					// settings.setTextZoom(textZoom);//设置字体大小
					break;
				case 1:
					settings.setTextSize(TextSize.LARGER);
					break;
				case 2:
					settings.setTextSize(TextSize.NORMAL);
					break;
				case 3:
					settings.setTextSize(TextSize.SMALLER);
					break;
				case 4:
					settings.setTextSize(TextSize.SMALLEST);
					break;

				default:
					settings.setTextSize(TextSize.NORMAL);
					break;
				}
				mCurrentItem = mCurrentChooseItem;
			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {

			}
		});

		builder.show();
	}

	/**
	 * 分享的方法
	 */
	private void showShare() {
		ShareSDK.initSDK(this);
		OnekeyShare oks = new OnekeyShare();
		// 设置主题
		oks.setTheme(OnekeyShareTheme.SKYBLUE);
		// 分享时Notification的图标和文字
		oks.setNotification(R.drawable.ic_launcher,
				getString(R.string.app_name));
		oks.setText("我是分享文本");
		oks.setImageUrl("http://f1.sharesdk.cn/imgs/2014/05/21/oESpJ78_533x800.jpg");
		// 启动分享GUI
		oks.show(this);
	}

}
